package com.example.test3;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Account {
    long getConversationTime(ZonedDateTime time, int fastConversionCount, int delayConversionCount, boolean hasConversion) {
        int month = time.getMonthValue();
        int day = time.getDayOfMonth();
        int hour = time.getHour();
        int dayOfWeek = time.getDayOfWeek().getValue();
        boolean isNeedSunday = false;
        if (month == 3 && dayOfWeek == 7 && day - 7 * fastConversionCount < 1 && hour >= 3 && hasConversion) {
            isNeedSunday = true;
            return time.toEpochSecond() - 60 * 60;
        }
        if (month == 11 && dayOfWeek == 7 && day - 7 * delayConversionCount < 1 && hasConversion) {
            return time.toEpochSecond() + 60 * 60;
        }
        return time.toEpochSecond();
    }
    public double calculate(String startTimeStr, String endTimeStr, int fastConversionCount, int delayConversionCount, boolean startHasConversion, boolean endHasConversion) {
        double costs = 0;
        LocalDateTime startTime = LocalDateTime.parse(startTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endTime = LocalDateTime.parse(endTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        ZonedDateTime startZonedTime = ZonedDateTime.of(startTime, ZoneId.systemDefault());
        ZonedDateTime endZonedTime = ZonedDateTime.of(endTime, ZoneId.systemDefault());
        long realEndTime = getConversationTime(endZonedTime, fastConversionCount, delayConversionCount, endHasConversion);
        long realStartTime = getConversationTime(startZonedTime, fastConversionCount, delayConversionCount, startHasConversion);
        double duration = Math.ceil((realEndTime - realStartTime) / 60.0);
        if (duration > 0 && duration <= 20.0) {
            costs = duration * 0.05;
        } else if (duration > 20.0) {
            costs = 1.0 + (duration - 20.0) * 0.10;
        }
        return costs;
    }
}
